package com.example.demo.utils;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * @author xieluoyin
 *  
 */
public class VerifyUtil {
    private VerifyUtil() {
    }

    public static void verifyStringEmpty(String fieldName, String fieldValue, String msg) {
        if (StringUtils.isEmpty(fieldValue)) {
            H3cIotExceptionUtil.parameterEMPTY(fieldName, msg);
        }
    }
    public static void verifyMatching(String fieldName, String sValue, String msg,String reg,String matchingMsg) {
        VerifyUtil.verifyStringEmpty(fieldName, sValue, msg);
        if (!sValue.matches(reg)) {
            H3cIotExceptionUtil.parameterMismatching(fieldName, msg, matchingMsg);
        }
    }

    public static void verifyObjectNull(String fieldName, Object object, String msg) {
        if (object == null) {
            H3cIotExceptionUtil.parameterNULL(fieldName, msg);
        }
    }


    private static void verifyMinGtMax(String fieldName, int min, int max, String message) {
        if (min > max) {
            H3cIotExceptionUtil.errorIsMinGtMax(fieldName, message);
        }
    }

    public static void verifyMax(String fieldName, int iValue, String msg, int min, int max) {
        VerifyUtil.verifyMinGtMax(fieldName, min, max, msg);
        if (iValue > max) {
            H3cIotExceptionUtil.exceedsUpperLimit(fieldName, iValue, msg, max);
        }
    }

    public static void verifyMin(String fieldName, int iValue, String msg, int min, int max) {
        VerifyUtil.verifyMinGtMax(fieldName, min, max, msg);
        if (iValue < min) {
            H3cIotExceptionUtil.exceedsLowerLimit(fieldName, iValue, msg, min);
        }
    }

    public static void verifyCollectionEmpty(String fieldName, Collection cValue, String message) {
        if (CollectionUtils.isEmpty(cValue)) {
            H3cIotExceptionUtil.parameterEMPTY(fieldName, message);
        }
    }
}
